{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-04-21 16:27:59 [INFO]\tStarting to read file list from dataset...\n",
      "2025-04-21 16:27:59 [INFO]\t1260 samples in file train.txt\n",
      "2025-04-21 16:27:59 [INFO]\tStarting to read file list from dataset...\n",
      "2025-04-21 16:27:59 [INFO]\t317 samples in file val.txt\n"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "from paddlex import transforms\n",
    "\n",
    "train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),\n",
    "                                    transforms.RandomHorizontalFlip(),\n",
    "                                    transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,contrast_range=0.9,contrast_prob=0.5,saturation_range=0.9,saturation_prob=0.5,hue_range=18,hue_prob=0.5),\n",
    "                                    transforms.Normalize()\n",
    "                                    ])\n",
    "\n",
    "val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n",
    "                                  transforms.CenterCrop(crop_size=224),\n",
    "                                  transforms.Normalize()\n",
    "                                  ])\n",
    "train_dataset=pdx.datasets.ImageNet(\n",
    "data_dir= 'garbage',\n",
    "file_list= 'train.txt',\n",
    "label_list= 'labels.txt',\n",
    "transforms=train_transforms,\n",
    "shuffle=True\n",
    ")\n",
    "\n",
    "val_dataset=pdx.datasets.ImageNet(\n",
    "data_dir='garbage',\n",
    "file_list='val.txt',\n",
    "label_list='labels.txt',\n",
    "transforms=val_transforms\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddlex as pdx\n",
    "num_classes = len(train_dataset.labels)\n",
    "model = pdx.cls.ResNet18(num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-04-21 16:28:02 [INFO]\tDownloading ResNet18_pretrained.pdparams from https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet18_pretrained.pdparams\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████| 69205/69205 [00:11<00:00, 5824.05KB/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-04-21 16:28:14 [INFO]\tLoading pretrained model from output/ResNet50_vd_ssld\\pretrain\\ResNet18_pretrained.pdparams\n",
      "2025-04-21 16:28:14 [WARNING]\t[SKIP] Shape of pretrained params fc.weight doesn't match.(Pretrained: (512, 1000), Actual: [512, 3])\n",
      "2025-04-21 16:28:14 [WARNING]\t[SKIP] Shape of pretrained params fc.bias doesn't match.(Pretrained: (1000,), Actual: [3])\n",
      "2025-04-21 16:28:14 [INFO]\tThere are 105/107 variables loaded into ResNet18.\n",
      "2025-04-21 16:28:58 [INFO]\t[TRAIN] Epoch=1/5, Step=10/78, loss=0.810822, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.35s, eta=0:28:58\n",
      "2025-04-21 16:29:40 [INFO]\t[TRAIN] Epoch=1/5, Step=20/78, loss=1.040216, acc1=0.437500, acc3=1.000000, lr=0.002000, time_each_step=4.22s, eta=0:27:24\n",
      "2025-04-21 16:30:22 [INFO]\t[TRAIN] Epoch=1/5, Step=30/78, loss=0.531752, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:26:41\n",
      "2025-04-21 16:31:04 [INFO]\t[TRAIN] Epoch=1/5, Step=40/78, loss=0.749084, acc1=0.625000, acc3=1.000000, lr=0.002000, time_each_step=4.24s, eta=0:26:7\n",
      "2025-04-21 16:31:47 [INFO]\t[TRAIN] Epoch=1/5, Step=50/78, loss=0.648924, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=4.23s, eta=0:25:21\n",
      "2025-04-21 16:32:29 [INFO]\t[TRAIN] Epoch=1/5, Step=60/78, loss=0.478208, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=4.24s, eta=0:24:44\n",
      "2025-04-21 16:33:11 [INFO]\t[TRAIN] Epoch=1/5, Step=70/78, loss=0.358972, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:23:52\n",
      "2025-04-21 16:33:45 [INFO]\t[TRAIN] Epoch 1 finished, loss=0.7552094, acc1=0.64903843, acc3=1.0 .\n",
      "2025-04-21 16:33:45 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 16:34:11 [INFO]\t[EVAL] Finished, Epoch=1, acc1=0.852404, acc3=1.000000 .\n",
      "2025-04-21 16:34:11 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 16:34:11 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_1, acc1=0.8524038195610046\n",
      "2025-04-21 16:34:11 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_1.\n",
      "2025-04-21 16:34:20 [INFO]\t[TRAIN] Epoch=2/5, Step=2/78, loss=0.887871, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=4.26s, eta=0:23:18\n",
      "2025-04-21 16:35:02 [INFO]\t[TRAIN] Epoch=2/5, Step=12/78, loss=0.522066, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:22:19\n",
      "2025-04-21 16:35:44 [INFO]\t[TRAIN] Epoch=2/5, Step=22/78, loss=0.250108, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:21:38\n",
      "2025-04-21 16:36:26 [INFO]\t[TRAIN] Epoch=2/5, Step=32/78, loss=0.215146, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:20:55\n",
      "2025-04-21 16:37:08 [INFO]\t[TRAIN] Epoch=2/5, Step=42/78, loss=0.576719, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:20:13\n",
      "2025-04-21 16:37:50 [INFO]\t[TRAIN] Epoch=2/5, Step=52/78, loss=0.487456, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:19:32\n",
      "2025-04-21 16:38:32 [INFO]\t[TRAIN] Epoch=2/5, Step=62/78, loss=0.384484, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:18:49\n",
      "2025-04-21 16:39:15 [INFO]\t[TRAIN] Epoch=2/5, Step=72/78, loss=0.263367, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=4.23s, eta=0:18:12\n",
      "2025-04-21 16:39:40 [INFO]\t[TRAIN] Epoch 2 finished, loss=0.53511643, acc1=0.7780449, acc3=1.0 .\n",
      "2025-04-21 16:39:40 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 16:40:06 [INFO]\t[EVAL] Finished, Epoch=2, acc1=0.906250, acc3=1.000000 .\n",
      "2025-04-21 16:40:06 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 16:40:06 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_2, acc1=0.90625\n",
      "2025-04-21 16:40:06 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_2.\n",
      "2025-04-21 16:40:23 [INFO]\t[TRAIN] Epoch=3/5, Step=4/78, loss=0.467341, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=4.25s, eta=0:17:8\n",
      "2025-04-21 16:41:05 [INFO]\t[TRAIN] Epoch=3/5, Step=14/78, loss=0.546450, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.22s, eta=0:16:21\n",
      "2025-04-21 16:41:48 [INFO]\t[TRAIN] Epoch=3/5, Step=24/78, loss=0.345962, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=4.23s, eta=0:15:40\n",
      "2025-04-21 16:42:30 [INFO]\t[TRAIN] Epoch=3/5, Step=34/78, loss=0.777490, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.22s, eta=0:14:55\n",
      "2025-04-21 16:43:12 [INFO]\t[TRAIN] Epoch=3/5, Step=44/78, loss=0.693540, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:14:12\n",
      "2025-04-21 16:43:54 [INFO]\t[TRAIN] Epoch=3/5, Step=54/78, loss=0.630862, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.21s, eta=0:13:29\n",
      "2025-04-21 16:44:36 [INFO]\t[TRAIN] Epoch=3/5, Step=64/78, loss=0.343205, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=4.2s, eta=0:12:46\n",
      "2025-04-21 16:45:19 [INFO]\t[TRAIN] Epoch=3/5, Step=74/78, loss=0.992021, acc1=0.375000, acc3=1.000000, lr=0.002000, time_each_step=4.3s, eta=0:12:20\n",
      "2025-04-21 16:45:36 [INFO]\t[TRAIN] Epoch 3 finished, loss=0.526267, acc1=0.7932692, acc3=1.0 .\n",
      "2025-04-21 16:45:36 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 16:46:02 [INFO]\t[EVAL] Finished, Epoch=3, acc1=0.853125, acc3=1.000000 .\n",
      "2025-04-21 16:46:02 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_2, acc1=0.90625\n",
      "2025-04-21 16:46:02 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_3.\n",
      "2025-04-21 16:46:29 [INFO]\t[TRAIN] Epoch=4/5, Step=6/78, loss=0.826654, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=4.34s, eta=0:11:17\n",
      "2025-04-21 16:47:20 [INFO]\t[TRAIN] Epoch=4/5, Step=16/78, loss=0.379014, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=5.15s, eta=0:12:27\n",
      "2025-04-21 16:48:32 [INFO]\t[TRAIN] Epoch=4/5, Step=26/78, loss=0.189867, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=7.13s, eta=0:15:53\n",
      "2025-04-21 16:49:21 [INFO]\t[TRAIN] Epoch=4/5, Step=36/78, loss=0.617541, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.94s, eta=0:10:18\n",
      "2025-04-21 16:50:03 [INFO]\t[TRAIN] Epoch=4/5, Step=46/78, loss=0.148003, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=4.24s, eta=0:8:12\n",
      "2025-04-21 16:50:51 [INFO]\t[TRAIN] Epoch=4/5, Step=56/78, loss=0.573763, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.75s, eta=0:8:21\n",
      "2025-04-21 16:51:33 [INFO]\t[TRAIN] Epoch=4/5, Step=66/78, loss=0.385587, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=4.22s, eta=0:6:45\n",
      "2025-04-21 16:52:16 [INFO]\t[TRAIN] Epoch=4/5, Step=76/78, loss=0.252345, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=4.31s, eta=0:6:10\n",
      "2025-04-21 16:52:25 [INFO]\t[TRAIN] Epoch 4 finished, loss=0.41716468, acc1=0.82532054, acc3=1.0 .\n",
      "2025-04-21 16:52:25 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 16:52:50 [INFO]\t[EVAL] Finished, Epoch=4, acc1=0.918750, acc3=1.000000 .\n",
      "2025-04-21 16:52:51 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 16:52:51 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_4, acc1=0.918749988079071\n",
      "2025-04-21 16:52:51 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_4.\n",
      "2025-04-21 16:53:33 [INFO]\t[TRAIN] Epoch=5/5, Step=8/78, loss=0.331808, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=5.07s, eta=0:5:54\n",
      "2025-04-21 16:54:24 [INFO]\t[TRAIN] Epoch=5/5, Step=18/78, loss=0.438377, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=5.11s, eta=0:5:6\n",
      "2025-04-21 16:55:22 [INFO]\t[TRAIN] Epoch=5/5, Step=28/78, loss=0.150851, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=5.8s, eta=0:4:49\n",
      "2025-04-21 16:56:23 [INFO]\t[TRAIN] Epoch=5/5, Step=38/78, loss=0.244740, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=6.06s, eta=0:4:2\n",
      "2025-04-21 16:57:26 [INFO]\t[TRAIN] Epoch=5/5, Step=48/78, loss=0.050768, acc1=1.000000, acc3=1.000000, lr=0.002000, time_each_step=6.29s, eta=0:3:8\n",
      "2025-04-21 16:58:15 [INFO]\t[TRAIN] Epoch=5/5, Step=58/78, loss=0.570438, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=4.99s, eta=0:1:39\n",
      "2025-04-21 16:59:08 [INFO]\t[TRAIN] Epoch=5/5, Step=68/78, loss=0.771601, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=5.25s, eta=0:0:52\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-04-21 16:59:57 [INFO]\t[TRAIN] Epoch=5/5, Step=78/78, loss=1.117838, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=4.96s, eta=0:0:0\n",
      "2025-04-21 16:59:57 [INFO]\t[TRAIN] Epoch 5 finished, loss=0.44353926, acc1=0.8293269, acc3=1.0 .\n",
      "2025-04-21 16:59:57 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 17:00:28 [INFO]\t[EVAL] Finished, Epoch=5, acc1=0.959375, acc3=1.000000 .\n",
      "2025-04-21 17:00:28 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 17:00:28 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_5, acc1=0.9593750238418579\n",
      "2025-04-21 17:00:28 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_5.\n"
     ]
    }
   ],
   "source": [
    "model.train(num_epochs=5,\n",
    "train_dataset=train_dataset,\n",
    "train_batch_size=16,\n",
    "eval_dataset=val_dataset,\n",
    "lr_decay_epochs=[80,100,150],\n",
    "save_interval_epochs=1,\n",
    "learning_rate=0.002,\n",
    "save_dir= 'output/ResNet50_vd_ssld',\n",
    "use_vdl=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-04-21 17:09:02 [ERROR]\tmodel_dir 'ResNet50_vd_ssld/best_model' does not exists!\n"
     ]
    },
    {
     "ename": "SystemExit",
     "evalue": "-1",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m -1\n"
     ]
    }
   ],
   "source": [
    "model = pdx.load_model( 'outputResNet50_vd_ssld/best_model')\n",
    "image_name = 'garbage.parper/parper10.jpg'\n",
    "result=model.predict(image_name)\n",
    "print( 'Predict Result:', result)\n",
    "number = result[0][ 'category']\n",
    "number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
